﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Windows.Forms.Design.Behavior;

namespace HML.Design
{
    /// <summary>
    /// TabPagePlus的设计模式行为
    /// </summary>
    internal class TabPagePlusDesigner : ScrollableControlDesigner
    {
        #region 重写属性

        public override SelectionRules SelectionRules
        {
            get
            {
                SelectionRules selectionRules = base.SelectionRules;
                Control control = this.Control;
                if (control != null && control.Parent is TabControlPlus)
                    selectionRules &= ~SelectionRules.AllSizeable;
                return selectionRules;
            }
        }

        #endregion

        public TabPagePlusDesigner()
        {
            this.AutoResizeHandles = true;
        }

        #region 重写方法

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);

            //清除在父容器停靠的智能标记
            DesignerActionService designerActionService = this.GetService(typeof(DesignerActionService)) as DesignerActionService;
            if (designerActionService != null)
            {
                designerActionService.Clear();
            }

        }

        protected override void OnPaintAdornments(PaintEventArgs pe)
        {
            base.OnPaintAdornments(pe);

            TabPagePlus tab_page = (TabPagePlus)this.Control;
            if (tab_page != null && tab_page.Parent != null && tab_page.Parent is TabControlPlus && ((TabControlPlus)tab_page.Parent).BodyBorderThickness == 0)
            {
                Pen borderPen = new Pen((double)this.Control.BackColor.GetBrightness() < 0.5 ? ControlPaint.Light(this.Control.BackColor) : ControlPaint.Dark(this.Control.BackColor))
                {
                    DashStyle = DashStyle.Dash
                };
                Rectangle clientRectangle = this.Control.ClientRectangle;
                --clientRectangle.Width;
                --clientRectangle.Height;
                pe.Graphics.DrawRectangle(borderPen, clientRectangle);
                borderPen.Dispose();
            }
        }

        public override bool CanBeParentedTo(IDesigner parentDesigner)
        {
            if (parentDesigner != null)
            {
                return parentDesigner.Component is TabControlPlus;
            }

            return false;
        }

        protected override ControlBodyGlyph GetControlGlyph(GlyphSelectionType selectionType)
        {
            this.OnSetCursor();
            return new ControlBodyGlyph(Rectangle.Empty, Cursor.Current, (IComponent)this.Control, (ControlDesigner)this);
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 公开鼠标拖载数据方法
        /// </summary>
        /// <param name="de"></param>
        internal void OnDragDropInternal(DragEventArgs de)
        {
            this.OnDragDrop(de);
        }
        /// <summary>
        /// 公开鼠标拖载数据方法
        /// </summary>
        /// <param name="de"></param>
        internal void OnDragEnterInternal(DragEventArgs de)
        {
            this.OnDragEnter(de);
        }
        /// <summary>
        /// 公开鼠标拖载数据方法
        /// </summary>
        /// <param name="e"></param>
        internal void OnDragLeaveInternal(EventArgs e)
        {
            this.OnDragLeave(e);
        }
        /// <summary>
        /// 公开鼠标拖载数据方法
        /// </summary>
        /// <param name="e"></param>
        internal void OnDragOverInternal(DragEventArgs e)
        {
            this.OnDragOver(e);
        }
        /// <summary>
        /// 公开鼠标拖载数据方法
        /// </summary>
        /// <param name="e"></param>
        internal void OnGiveFeedbackInternal(GiveFeedbackEventArgs e)
        {
            this.OnGiveFeedback(e);
        }

        #endregion
    }
}
